
import UIKit
import Toast_Swift

//MARK: - 弱提示类型
enum ToastType {
    /// 成功提示
    case success
    /// 发生错误提示
    case error
    /// 等待（此时是指示器在转动）
    case waiting
    
    var image: UIImage? {
        switch self {
        case .success: return UIImage(named: "")
        case .error: return UIImage(named: "")
        case .waiting: return nil
        }
    }
    var contentColor: UIColor {
        return UIColor.black
    }
}

class Toast_View_Tool: NSObject {
    
    class func dismiss() {
        DispatchQueue.main.async {
            UIApplication.shared.keyWindow?.subviews.forEach {
                if $0.tag == 7813413 {$0.removeFromSuperview()}
            }
        }
    }
    
    class func dismiss(_ afterDelay: Double) {
        DispatchQueue.main.asyncAfter(deadline: .now()+afterDelay) {
            UIApplication.shared.keyWindow?.subviews.forEach {
                if $0.tag == 7813413 {$0.removeFromSuperview()}
            }
        }
    }
    
    class func show(type: ToastType = .waiting, text: String? = "", position: ToastPosition = .top, title: String = "", waitingBackAlpha: CGFloat = 0.3) {
        
        ToastManager.shared.isQueueEnabled = true
        ToastManager.shared.isTapToDismissEnabled = false
        
        DispatchQueue.main.async {
            
            if let window = UIApplication.shared.keyWindow {
                    if type == .waiting {
                        let view = LoadingView(text ?? "", waitingBackAlpha)
                        window.addSubview(view)
                        view.snp.makeConstraints { (make) in
                            make.edges.equalToSuperview()
                        }
                    }else {
                        dismiss()
                        if !(text ?? "").isEmpty {
                            let view = makeToast(text ?? "")
                            window.showToast(view, duration: 2.0, point: .zero, completion: nil)
                            if view.superview == nil {
                                window.addSubview(view)
                            }
                            view.snp.makeConstraints { make in
                                make.top.equalTo(StatusBarHeight)
                                make.centerX.equalToSuperview()
                            }
                        }
                    }
            }
        }
    }
    
    private class func makeToast(_ message: String) -> UIView {
        let label = AntInsetLabel()
        label.paddingLeft = 15.Fit
        label.paddingRight = 15.Fit
        label.paddingTop = 10.Fit
        label.paddingBottom = 10.Fit
        label.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        label.textColor = .white
        label.font = .setTypeFace(14, type: .medium)
        label.textAlignment = .center
        /// 内部处理
//        label.layer.cornerRadius = 20.Fit
        label.layer.masksToBounds = true
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = screenWidth - 100.Fit
        label.adjustsFontSizeToFitWidth = true
        label.text = message
        return label
    }
    
}

fileprivate class AntInsetLabel : UILabel {
    private var padding = UIEdgeInsets.zero
    
    var paddingLeft: CGFloat { get { return padding.left } set { padding.left = newValue } }
    
    var paddingRight: CGFloat { get { return padding.right } set { padding.right = newValue } }
    
    var paddingTop: CGFloat { get { return padding.top } set { padding.top = newValue } }
    
    var paddingBottom: CGFloat { get { return padding.bottom } set { padding.bottom = newValue } }
    
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
    }
    
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let insets = self.padding
        var rect = super.textRect(forBounds: bounds.inset(by: insets), limitedToNumberOfLines: numberOfLines)
        rect.origin.x -= insets.left
        rect.origin.y -= insets.top
        rect.size.width += (insets.left + insets.right)
        rect.size.height += (insets.top + insets.bottom)
        
        if rect.size.height < 40.Fit {
            self.layer.cornerRadius = rect.size.height/2
        }else {
            self.layer.cornerRadius = 20.Fit
        }
        return rect
    }
    
}

